package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dkd.common.constant.VmStatus;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.bean.BeanUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.service.IChannelService;
import com.dkd.manage.service.INodeService;
import com.dkd.manage.service.IVmTypeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author ruruihan
 * @date 2025-11-04
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Resource
    private VendingMachineMapper vendingMachineMapper;
    @Resource
    private IVmTypeService vmTypeService;
    @Resource
    private INodeService nodeService;
    @Resource
    private IChannelService channelService;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //新增设备
        String innerCode = UUIDUtils.generateDeviceId();
        vendingMachine.setInnerCode(innerCode);
        //通过typeId查询设备的容量 //同步更新设备的容量
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        //通过nodeId查询合作商id,addr,商圈类型,区域id //同步更新设备的addr,商圈类型,区域id,合作商id
        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
        BeanUtils.copyProperties(node,vendingMachine,"id"); //商圈类型,区域id,合作商id
        vendingMachine.setAddr(node.getAddress()); //设备地址
        //设备状态
        vendingMachine.setVmStatus(VmStatus.NOT_PUT_AWAY); //设备状态
        vendingMachine.setCreateTime(DateUtils.getNowDate()); //创建时间
        vendingMachine.setUpdateTime(DateUtils.getNowDate()); //更新时间
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        //新增货道  双重for循环
        List<Channel> channelList = new ArrayList<>() ;
        for (int i = 1; i <= vmType.getVmRow(); i++) {
            for (int j = 1; j <= vmType.getVmCol(); j++) {
                Channel channel = new Channel();
                channel.setChannelCode(i + "_" + j);
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(vendingMachine.getInnerCode());
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channelList.add(channel);
            }
        }
        channelService.insertChannelBatch(channelList);

        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        //判断是更改设备点位还是更改设备的策略
        if(vendingMachine.getNodeId() != null){
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            BeanUtils.copyProperties(node,vendingMachine,"id");
            vendingMachine.setAddr(node.getAddress());
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        //构造查询条件
        LambdaQueryWrapper<VendingMachine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VendingMachine::getInnerCode,innerCode);
        return vendingMachineMapper.selectOne(queryWrapper);
    }
}
